package com.rjwh.dingdong.client.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.aishua.lib.utils.AsStrUtils;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.rjwh.dingdong.client.R;
import com.rjwh.dingdong.client.bean.localbean.DrawBookCommentData;
import com.rjwh.dingdong.client.util.ViewHolder;
import com.rjwh.dingdong.client.widget.RoundImageView;

/**
 * @author Kay
 * @ClassName DrawBookDetailCommentAdapter
 * @Description 绘本评论列表
 * @date 2016-8-10
 * @history 2016-8-10 author: description:
 */
public class DrawBookDetailCommentAdapter extends AdapterBase<DrawBookCommentData> {

    private Context context;
    private boolean isDetial;

    public DrawBookDetailCommentAdapter(Context context, boolean isDetial) {
        this.context = context;
        this.isDetial = isDetial;
    }

    @Override
    protected View getExView(int position, View convertView, ViewGroup parent) throws Exception {
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.draw_book_detail_comment_item, null);
        }
        RoundImageView imgRv = ViewHolder.get(convertView, R.id.draw_book_detail_comment_item_img);
        ImageView levelImg = ViewHolder.get(convertView, R.id.draw_book_detail_comment_item_user_level_img);
        TextView nameTv = ViewHolder.get(convertView, R.id.draw_book_detail_comment_item_name_tv);
        TextView timeTv = ViewHolder.get(convertView, R.id.draw_book_detail_comment_item_time_tv);
        TextView contentTv = ViewHolder.get(convertView, R.id.draw_book_detail_comment_item_content_tv);
        View topDiveder = ViewHolder.get(convertView, R.id.draw_book_detail_comment_item_top_divider);
        View bottomDiveder = ViewHolder.get(convertView, R.id.draw_book_detail_comment_item_bottom_divider);
        DrawBookCommentData drawBookCommentData = getList().get(position);

        if (isDetial) {
            bottomDiveder.setVisibility(View.GONE);
            topDiveder.setVisibility(View.VISIBLE);
        } else {
            topDiveder.setVisibility(View.GONE);
            bottomDiveder.setVisibility(View.VISIBLE);
        }

        if (drawBookCommentData == null) {
            return convertView;
        }
        String avatar = drawBookCommentData.getAvatar();
        String name = drawBookCommentData.getName();
        String content = drawBookCommentData.getContent();
        String time = drawBookCommentData.getTime();
        String userlevel = drawBookCommentData.getUserlevel();

        if (avatar != null) {
            Glide.with(context).load(avatar).dontAnimate().placeholder(R.drawable.icon_defult_avatar).error(R.drawable.icon_defult_avatar).diskCacheStrategy(DiskCacheStrategy.ALL).into(imgRv);
        }
        if (!AsStrUtils.isEmpty(name)) {
            nameTv.setText(name);
        }
        if (!AsStrUtils.isEmpty(content)) {
            contentTv.setText(content);
        }
        if (!AsStrUtils.isEmpty(time)) {
            timeTv.setText(time);
        }
        if (!AsStrUtils.isEmpty(userlevel)) {
            setLevelIcon(levelImg, Integer.parseInt(userlevel));
        }
        return convertView;
    }

    private void setLevelIcon(ImageView imageView, int level) {
        int id = 0;
        switch (level) {
            case 1:
                id = R.drawable.icon_level_v1;
                break;
            case 2:
                id = R.drawable.icon_level_v2;
                break;
            case 3:
                id = R.drawable.icon_level_v3;
                break;
            case 4:
                id = R.drawable.icon_level_v4;
                break;
            case 5:
                id = R.drawable.icon_level_v5;
                break;
            case 6:
                id = R.drawable.icon_level_v6;
                break;
            default:
                break;
        }
        imageView.setImageResource(id);
    }
}
